﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;

namespace Client
{
    public partial class FormREG : Form
    {
        //define opcode
        const int LOGIN = 0;
        const int REGISTRATION = 1;

        private TcpClient tcpClient;

        public FormREG(TcpClient tcpClient)
        {
            InitializeComponent();
            this.tcpClient = tcpClient;
        }

        private void buttonReg_Click(object sender, EventArgs e)
        {
            if (textBoxPass.Text != textBoxRePass.Text)
            {
                MessageBox.Show("Password does not match!", "Error");
                return;
            }
            byte[] buffer = BitConverter.GetBytes(REGISTRATION);
            NetworkStream ns = tcpClient.GetStream();
            ns.Write(buffer, 0, buffer.Length); //send opcode

            byte[] lenBuffer = BitConverter.GetBytes(textBoxUsername.Text.Length);
            ns.Write(lenBuffer, 0, 4);//send length of username
            buffer = Encoding.ASCII.GetBytes(textBoxUsername.Text);
            ns.Write(buffer, 0, buffer.Length);//send username

            lenBuffer = BitConverter.GetBytes(textBoxPass.Text.Length);
            ns.Write(lenBuffer, 0, 4);//send length of pass
            buffer = Encoding.ASCII.GetBytes(textBoxPass.Text);
            ns.Write(buffer, 0, buffer.Length);//send pass

            lenBuffer = BitConverter.GetBytes(textBoxRePass.Text.Length);
            ns.Write(lenBuffer, 0, 4);//send length of repass
            buffer = Encoding.ASCII.GetBytes(textBoxRePass.Text);
            ns.Write(buffer, 0, buffer.Length);//send repass
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
